package conntion

import (
	"fmt"
	"net"
	"tcp_server/conf"
)

var listenr *net.TCPListener
var connections = make(map[string]Connection)

type Connection struct {
	UserId   string
	Password string
	Name     string
	Conn     net.TCPConn
}

func Server(listen *net.TCPListener) {
	for {
		conn, err := listen.AcceptTCP()
		if err != nil {
			fmt.Println("accept client error:", err.Error())
			continue
		}
		fmt.Println("accept client :" + conn.RemoteAddr().String())
	}
}

func Listen() {
	fmt.Println("tcpconn init start")
	var address = new(net.TCPAddr)
	address.IP = net.ParseIP(conf.GetConfig().Host)
	address.Port = conf.GetConfig().Port
	listenr, err := net.ListenTCP("tcp", address)
	if err != nil {
		fmt.Println("listen error:" + err.Error())
	} else {
		Server(listenr)
	}
}
